<template>

    <div id="medical-record" class="w-h-100 pos-r flex-1 apply-container page-public-s" style="box-sizing: border-box;overflow:hidden;">
        <div class="w-h-100 ver-flex pos-a" style="box-sizing: border-box; padding: 6px;">
            <div style="box-sizing: border-box;">
                <el-card shadow="never" :body-style="{ paddingBottom: '0' }">
                    <el-form class="box-f" ref="queryForm" :inline="true">
                        <el-form-item :label="$t('b0t63v7bGsmbXkQdwW4Q0')">
                            <el-select 
                                v-model="searchVal" 
                                filterable 
                                class="m-2" 
                                :placeholder="$t('g6AmUBb2H8qwj8DuC0TH')" 
                                @change="searchChange"
                            >
                                <el-option
                                    v-for="item in searchOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <!-- 模糊查询 -->
                            <template v-if="searchVal === '1'">
                                <el-input v-model="transferVal" :placeholder="$t('yeyw245R7a6VYy19Otp1n')" />
                            </template>
                            <!-- 是否启用 -->
                            <template v-else-if="searchVal === '2'">
                                <el-select 
                                    v-model="transferVal" 
                                    filterable 
                                    class="m-2" 
                                    :placeholder="$t('4qSllUxIkPaA8Rqf3Jg8y')" 
                                    @change="acquireChange"
                                    value-key="id"
                                >
                                    <el-option
                                        v-for="item in responsiblePle"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </template>
                            <!-- 时间段查询 -->
                            <template v-else>
                                <el-date-picker
                                    v-model="transferVal"
                                    type="datetimerange"
                                    range-separator="-"
                                    :start-placeholder="$t('zxGaloajaAo1yMttcStime')"
                                    :end-placeholder="$t('zxGaloajaAo1yMttcEtime')"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    popper-class="el-date-p"
                                />
                            </template>
                        </el-form-item>
                        <el-form-item >
                            <el-button @click='searchClick' type="primary" icon="ele-Search"> {{ $t('a4Uw6qeAZpctNhl3u7Uge') }} </el-button>
                            <el-button @click="resetClick" icon="ele-Refresh"> {{ $t('rxCkXeM2sO75ltcf8JyVj') }} </el-button>
                            <el-button v-if="$hasPer('medicalRecord:add')" @click="nodeClick('')" icon="ele-Plus" type="primary"> {{ $t('kfR7oKqGr0sNpRoAhHcM') }} </el-button>
                            <el-button v-if="$hasPer('medicalRecord:xzmb')" style="margin-right: 10px;" icon="ele-Download" @click="downClick">{{ $t('sYi7Mf3clSks49qJa_2Q') }}</el-button>
                            <el-upload
                                v-if="$hasPer('medicalRecord:dr')"
                                class="upload-demo"
                                action="#"
                                :auto-upload='false'
                                ref="aModel"
                                :show-file-list="false"
                                :on-change="( file:any ) => { ExcelChange( file , getData ) }"
                                :on-success="onSuccess"
                                :limit="1"
                            >
                                <el-button icon="ele-Upload">{{ $t('1ec5uPhDhD6K2aFDlsEll') }}</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>

            <div style="box-sizing: border-box;" class="flex-1 pos-r">
                <div class="pos-a w-h-100" style="box-sizing: border-box;">
                    <el-card class="box-card w-h-100" shadow="never" style="margin-top: 16px;">
                        <div style="" class="w-h-100 ver-flex">
                            <div class="flex-1 pos-r">
                                <div class="pos-a w-h-100">

                                    <el-table :data="list" class="w-h-100" style="width: 100%" border v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.5)">
                                        
                                        <el-table-column type="index" :label="$t('3eKEs1bExAXarYpdJemp')" width="55" align="center" :index="indexMethod" />
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('8DpGlcrrBmGlRtHfCt5Rh')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('8DpGlcrrBmGlRtHfCt5Rh')" v-model="searchVar.v_PhysicalEmpName" />
                                                </template>
                                                <template #default="scope">
                                                    <div>{{ scope.row.v_PhysicalEmpName }}</div>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column width="430" :formatter="formatDate" :label="$t('93LtoXhXbJz_9ofgpcIl')"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" width="430" align="center">
                                                <template #header>
                                                    <el-date-picker
                                                        v-model="transferVal"
                                                        type="datetimerange"
                                                        range-separator="-"
                                                        :start-placeholder="$t('zxGaloajaAo1yMttcStime')"
                                                        :end-placeholder="$t('zxGaloajaAo1yMttcEtime')"
                                                        value-format="YYYY-MM-DD HH:mm:ss"
                                                        popper-class="el-date-p"
                                                    />
                                                </template>
                                                <template #default="scope">
                                                    <div>{{ scope.row.d_PhysicalTime }}</div>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('hLhs6kGhRuuQv4TeA2zI')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('hLhs6kGhRuuQv4TeA2zI')" v-model="searchVar.v_PhysicalAddress" />
                                                </template>
                                                <template #default="scope">
                                                    <div>{{ scope.row.v_PhysicalAddress }}</div>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('lh37CfWmpMkwgcRxBtw2O')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('lh37CfWmpMkwgcRxBtw2O')" v-model="searchVar.v_PhysicalResult" />
                                                </template>
                                                <template #default="scope">
                                                    <div>{{ scope.row.v_PhysicalResult }}</div>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('hQmmlRmkje2IkDoxRZjj')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-input type="text" :placeholder="$t('hQmmlRmkje2IkDoxRZjj')" v-model="searchVar.v_WorkTypeName" />
                                                </template>
                                                <template #default="scoped">
                                                    <span v-if="scoped.row.v_WorkTypeName">{{ scoped.row.v_WorkTypeName }}</span>
                                                    <span v-else>-</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('4qSllUxIkPaA8Rqf3Jg8y')" align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="160" align="center">
                                                <template #header>
                                                    <el-select 
                                                        v-model="transferVal" 
                                                        filterable 
                                                        class="m-2" 
                                                        :placeholder="$t('4qSllUxIkPaA8Rqf3Jg8y')" 
                                                        @change="acquireChange"
                                                        value-key="id"
                                                    >
                                                        <el-option
                                                            v-for="item in responsiblePle"
                                                            :key="item.value"
                                                            :label="item.label"
                                                            :value="item.value"
                                                        />
                                                    </el-select>
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.n_isValid === 1" style="color: #07AAF9;">{{ $t('iPfonvtxVLm7knSyCsLBo') }}</span>
                                                    <span v-if="scope.row.n_isValid === 2" style="color: #FF5F33;">{{ $t('tzm8JwYsKnqKoNhheJlJx') }}</span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" :label="$t('1aMo3JvnexQYlM0YqllPc')" align="center">
                                            <template #default="scope">
                                                <span style="cursor: pointer;" @click="fileClick(scope.row.v_FilePath)" v-if="scope.row.v_FilePath">{{ $t('2KyEk7Ir2Lz3bN9ByjSaE') }}</span>
                                                <span v-else>{{ $t('tzv7Fao_8LvfFbddIkvDu') }}</span>
                                            </template>
                                        </el-table-column>
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" prop="createdTime" :label="$t('xxAj6fsjvIsQrMdlNpolv')" align="center" />
                                        
                                        <el-table-column min-width="160" :show-overflow-tooltip="true" prop="createdUserName" :label="$t('v7AhfaNeGaFoPfZzgpFk')" align="center" />
                                        
                                        <el-table-column prop="v_Remark" min-width="160" :show-overflow-tooltip="true" :label="$t('9RLaVEiJf1Z1Qv15o6Kll')" align="center" />
                                        
                                        <el-table-column min-width="160" :label="$t('liBmO8wb58NMw5HLu9Nf')" align="center" fixed="right">
                                            <template #default="scope">
                                                <el-button v-if="$hasPer('medicalRecord:edit')" @click="nodeClick(scope.row)" class="icon-e" icon="ele-Edit" size="small" text type="primary"> {{ $t('qdjaUu5q3JYq0bnq8l') }} </el-button>
                                                <el-button v-if="$hasPer('medicalRecord:del')" @click="deleteHandle(scope.row.n_Id , getData)" class="icon-e" icon="ele-Delete" size="small" text type="danger"> {{ $t('mkwlMbm3qr88f0XxV9Pje') }} </el-button>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </div>
                            </div>
                            <div style="height: 30px;margin-top: 20px;" class="hr-flex-start">
								<el-pagination
									v-model:currentPage="pageNo"
									v-model:page-size="pageSize"
									:total="totalRows"
									:page-sizes="[10, 20, 50, 100]"
									small
									background
									@size-change="handleSizeChange"
									@current-change="handleCurrentChange"
									layout="total, sizes, prev, pager, next, jumper"
									class="w-h-100"
								/>
							</div>
                        </div>
                    </el-card>
                </div>
            </div>
        </div>
        <add-edit-medical-record
            :repairInfo="repairInfo"
            :isUseDialog="isUseDialog"
            @updateData="getData"
            @cancelUseClick="cancelUseClick"
            @controlUseClick="controlUseClick"
        />
        <VModel
            :fileInfo="fileInfo"
            :isModelDialog="isModelDialog"
            @cancelModelClick="cancelModelClick"
        />
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, onMounted , reactive , toRaw } from 'vue'
import getTableData from '/@/utils/getTableData'
import { PhysicalRecordPage , PhysicalRecordDelete , UploadBatchImportData } from '/@/api/sms/sms'
import AddEditMedicalRecord from './component/add-edit-medical-record.vue'
import { ElMessageBox , ElMessage } from 'element-plus'
import useCurrentInstance from '/@/utils/hook/useCurrentInstance';
import VModel from '../components/v-model.vue';
export default defineComponent( {
    name: 'medical-record',
    components: {
        AddEditMedicalRecord,
        VModel,
    },
    setup(props){
        const { proxy } = useCurrentInstance();
        const $t = proxy.$t;
        
        const isUseDialog = ref() // 新增/编辑弹出框隐藏/显示
        const repairInfo = ref() // 传递子组件数据
        const pathData = ref(import.meta.env.VITE_API_URL + '/') // 获取地址
        const aModel:Ref = ref(null)
        const inputValue = ref();

        const isModelDialog = ref(false);
        const cancelModelClick = () => {isModelDialog.value = false};
        const fileInfo = ref('');

        /*** 查看文件 */
        const fileClick = (val: any) => {
            isModelDialog.value = true;
            let setObj: any = {};
            setObj.titleInfo = $t('1aMo3JvnexQYlM0YqllPc');
            setObj.rectifyListArr = val;
            fileInfo.value = JSON.stringify(setObj);
        }

        const searchVar = reactive({
            v_PhysicalEmpName: '',
            d_PhysicalTime: '',
            v_PhysicalAddress: '',
            v_PhysicalResult: '',
            v_WorkTypeName: '',
            n_isValid: ''
        });


        // 搜索类
        const searchVal = ref('1')
        const transferVal = ref() // 需要查询的值
        let searchObj:any = reactive({})
        let searchName = '1'
        /*** resetFun */
        const resetFun = () => {
            let newObj = Object.keys(searchObj)
            if(newObj.length !== 0){
                newObj.map((i:any) => {
                    searchObj[i] = ''
                })
            }
        }
        /*** 搜索类型选择事件 searchChange */
        const searchChange = (val:any) => {
            searchName = val
            transferVal.value = ''
        }
        /*** acquireChange 搜索下拉框选择的数据 ---- 传给接口数据 */
        const acquireChange = (val:any) => {
            console.log(val)
        }
        /*** searchClick 查询按钮 */
        const searchClick = () => {
            resetFun()
            if(searchName === '1'){ searchObj.searchValue = transferVal.value }
            if(searchName === '2'){ searchObj.n_isValid = transferVal.value }
            if(searchName === '3'){ 
                searchObj.startTime = toRaw(transferVal.value)[0] 
                searchObj.endTime = toRaw(transferVal.value)[1]
            } 
        }
        /*** resetClick 重置按钮 */
        const resetClick = () => {
            resetFun()
            transferVal.value = ''
            searchVal.value = '1'
        }
        /** 每行需要查询的事件 */
        const handleInput = (row: any, val: string) => {
            console.log(row,val)
        }
        // 搜索类

        /*** 截取日期格式方法 */
        const formatDate = (row:any, column:any) => {
          // 获取单元格数据
          let data = row[column.property]
          if(data === null || data == $t('utiIUzqSzcKy0QiDc49n')){
            return '-'
          }else{
            return data
          }
        }

        /*** nodeClick 新增/编辑点击事件 */
        const nodeClick = (val:string | object) => {
            val !== '' ? repairInfo.value = JSON.stringify(toRaw(val)) : repairInfo.value = ''
            isUseDialog.value = true
        }
        /*** deleteHandle 删除数据事件 */
        const deleteHandle = async (val: String | Number , updateFun: Function) => {
            ElMessageBox.confirm($t('jj0tHyWhJ4Frf44t43S4c'),$t('99FoyBoPtExRdppuYpkx'),
                {
                    confirmButtonText: $t('cpmrq09Mw3tfVmwWGsZzb'),
                    cancelButtonText: $t('wFa44E4Iz025Ha2gYeDop'),
                    type: 'warning',
                }
            )
            .then(async() => {
                let param = {n_Id : val}
                try {
                    let res:any = await PhysicalRecordDelete(param)
                    if(res.success){
                        updateFun()
                    }
                } catch (err) { return }
            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: $t('ummczgwDCP3jaIVczZic'),
                })
            })
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            isUseDialog.value = false
        }

        /*** controlUseClick 提交事件 */
        const controlUseClick = () => {
            isUseDialog.value = false
        }

        /*** downClick 下载模板 */
        const downClick = () => {
            window.location.href = window.$host + '/Upload/SMS/Batch/PhysicalRecord.xlsx' // 覆盖当前页
            // window.open(window.$host + '/Upload/SMS/Batch/PhysicalRecord.xlsx','_blank') // 打开新页面
        }

        /*** ExcelChange 导入Excel */
        const ExcelChange = (item:any , updateFun: Function) => {
            let reader = new FileReader()
            reader.readAsDataURL(item.raw)
            reader.onload = (e:any) => {
                let base64String = e.target.result;
                let param:any = new Object()
                param.ModelType = 'PhysicalRecord'
                param.FileContent = base64String
                uploadFun(param , updateFun)
            }
        }
        /*** uploadFun 批量导入数据接口 */
        const uploadFun = async ( p: Object , updateFun: Function ) => {
            aModel.value.clearFiles()
            let res:any = await UploadBatchImportData(p)
            if(res.success) updateFun() 
        }

        /*** onSuccess 文件上传成功时的钩子函数 */
        const onSuccess = (item:any) => {
            console.log(item,'item')
        }

        return {
            ...getTableData(PhysicalRecordPage,{},searchObj),
            isUseDialog,
            cancelUseClick,
            controlUseClick,
            nodeClick,
            repairInfo,
            deleteHandle,

            aModel,
            downClick,
            ExcelChange,
            onSuccess,
            inputValue,


            // 搜索类
            searchVal,
            transferVal,
            resetClick,
            searchClick,
            searchChange,
            acquireChange,
            searchOptions: [{
                value: '1',
                label: $t('3dBd6Gp5MqNm4A5dvcqJ4')
            }, {
                value: '2',
                label: $t('4qSllUxIkPaA8Rqf3Jg8y')
            }, {
                value: '3',
                label: $t('bxOzlOcGrW1Oanu81ddgi')
            }], // 搜索方式
            responsiblePle: [
                { label: $t('iPfonvtxVLm7knSyCsLBo') , value: 1 },
                { label: $t('tzm8JwYsKnqKoNhheJlJx') , value: 2 },
            ], // 是否启用
            // 搜索类


            pathData,
            formatDate,
            handleInput,
            searchVar,

            isModelDialog,
            cancelModelClick,
            fileInfo,
            fileClick,
        }
    },
} )
</script>
<style lang="scss"> @import '../style/public.scss'; </style>
<style scoped lang="scss">
    #medical-record {
        .box-f{
            :deep(.el-form-item__label){
                padding: 0;
            }
        }
        :deep(.el-card__body) {
            height: 100%;
        }
        :deep(.el-card){
            border: none !important;
        }
        ul {

            list-style: none;

            li {
                cursor: pointer;

                &:hover {
                    color: red;
                }
            }
        }
        .icon-e{
            :deep(.el-icon){
                margin-right: 0px!important;
            }
        }
    }
</style>